import axios from 'axios'
import { getToken } from '@/assets/js/auth'
import router from '@/router/index'
// 序列化请求
import qs from 'qs'
import { Toast, Indicator } from 'mint-ui'
export const http = axios.create({
  baseURL: process.env.VUE_APP_API_URL,
  timeout: 15000,
})
// 请求拦截器
http.interceptors.request.use(config => {
  if (config.url !== '/login') { // 判断是不是 "/login" 路径,不是的话就加入token
    config.headers['token'] = getToken()
  }
  return config
}, error => {
  return Promise.reject(error)
})

// 响应拦截器
http.interceptors.response.use(response => {
  Indicator.close()
  const data = response.data
  switch (data.code) {
    case "0":
      break
    case "100117": // token 过期
      Toast({
        message: data.msg,
        position: 'middle',
        duration: 3000
      })
      // 跳转登录
      setTimeout(() => {
        router.push({
          name: 'login',
          query: {
            redirect: window.location.hash
          }
        })
      },3000)
      break
    case "100118": // token 过期
      Toast({
        message: data.msg,
        position: 'middle',
        duration: 3000
      })
      // 跳转登录
      setTimeout(() => {
        router.push({
          name: 'login',
          query: {
            redirect: window.location.hash
          }
        })
      },3000)
      break
    case "100119": // 另外设备登录
      Toast({
        message: data.msg,
        position: 'middle',
        duration: 3000,
      })
      // 跳转登录
      setTimeout(() => {
        router.push({
          name: 'login',
          query: {
            redirect: window.location.hash
          }
        })
      },3000)
      break
  }
  return response
}, error => {
  Indicator.close()
  const resCode = error.response.status
  switch (resCode) {
    case 500:
      Toast({
        message: '当前无网络，请检查网络设置',
        position: 'middle',
        duration: 3000,
      });
      break
    default:
      Toast({
        message: "网络不畅，请检查网络设置",
        position: 'middle',
        duration: 3000,
      });
  }
  return Promise.reject(error)
})
/**
 * 封装get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    http
      .get(url, {
        params: params
      })
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * post方法，对应post请求====> post请求的序列化参数==>比如 "url?id=1234567&code=miaozi"
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
function post(url, params) {
  return new Promise((resolve, reject) => {
    http
      .post(url, qs.stringify(params))
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}
// JSON 请求
function json(url, params) { // 也是post请求,传的参数是FormData==> {id:123456,name:miaozi}
  return new Promise((resolve, reject) => {
    http
      .post(url, params)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}
export {
  get,
  post,
  json
}